package com.hss01248.facedetect.view;

import android.content.Context;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.speech.tts.TextToSpeech;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.Camera;
import com.faceplusplus.api.FaceDetecter.Face;
import com.hss01248.facedetect.FaceTrackListener;
import com.hss01248.facedetect.ShootListener;
import com.hss01248.facedetect.activity.CollectActivity;
import com.hss01248.facedetect.activity.FaceTrackActivity;
import com.hss01248.facedetect.utils.SHExtends;
import com.hss01248.facedetect.utils.ToastUtil;

import java.util.Arrays;

public class FaceMask extends View {
    ShootListener listener;
    FaceTrackListener faceTrackListener;
    FaceTrackActivity factivity;
    CollectActivity collectActivity;
    TextToSpeech mSpeech;
    ImageView img;
    Camera camera;
    TextView tv_ticketNo;
    TextView tv_message;
    ImageView img_result;
    private Paint localPaint;
    private Face[] faceInfos;
    private RectF rect;
    boolean isCollectShoot=true;
    FaceMask faceMask;
    SHExtends she;
    int count=0;

    public FaceMask(Context context, AttributeSet atti) {
        super(context, atti);
        rect = new RectF();
        localPaint = new Paint();
        localPaint.setColor(0xff248FAF);
        localPaint.setStrokeWidth(10);
        localPaint.setStyle(Paint.Style.STROKE);
    }

    public void setFaceInfo(Face[] faceInfos) {
        this.faceInfos = faceInfos;
        invalidate();



    }

    public void setCamera(Camera camera,FaceMask faceMask,FaceTrackActivity activity,TextView tv_ticketNo,TextView tv_message, ImageView img_result,TextToSpeech mSpeech,SHExtends she) {
        this.camera = camera;
        this.faceMask=faceMask;
        this.tv_message=tv_message;
        this.tv_ticketNo=tv_ticketNo;
        this.img_result=img_result;
        this.mSpeech=mSpeech;
        factivity=activity;
        faceTrackListener= new FaceTrackActivity();
        this.she=she;
    }

    public void setCamera(Camera camera, ImageView img,CollectActivity activity,SHExtends she) {
        listener=new CollectActivity();
        this.camera = camera;
        this.img=img;
        this.she=she;
        collectActivity=activity;
    }

    public  void setShoot(){
        isCollectShoot=true;
        invalidate();
//        Log.d("shoot",""+count++);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (faceInfos == null)
            return;
        for (Face localFaceInfo : faceInfos) {

            rect.set(getWidth() * localFaceInfo.left, getHeight() * localFaceInfo.top,
                    getWidth() * localFaceInfo.right, getHeight() * localFaceInfo.bottom);

//            for (int i = 0; i < faceInfos.length; i++) {
//                Arrays.sort(faceInfos);
//
//
//
//            }
            canvas.drawRect(rect, localPaint);



            if (isCollectShoot) {


                if (listener!=null) {
                    listener.passShoot(camera ,img,collectActivity,she);
                }

                if (faceTrackListener!=null) {
                    faceTrackListener.PassCamera(camera,faceMask,factivity,tv_ticketNo,tv_message,img_result,mSpeech,she);
                }


                isCollectShoot=false;
            }

        }
    }
}
